home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1NLQD7U (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  13.4 KB  |  481 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.accessibility.Accessible;
  4. import com.sun.java.accessibility.AccessibleContext;
  5. import com.sun.java.accessibility.AccessibleState;
  6. import com.sun.java.swing.event.ChangeEvent;
  7. import com.sun.java.swing.event.ChangeListener;
  8. import com.sun.java.swing.event.EventListenerList;
  9. import com.sun.java.swing.plaf.TabbedPaneUI;
  10. import java.awt.Color;
  11. import java.awt.Component;
  12. import java.awt.Container;
  13. import java.awt.Rectangle;
  14. import java.awt.event.MouseEvent;
  15. import java.io.Serializable;
  16. import java.util.Vector;
  17.  
  18. public class JTabbedPane extends JComponent implements Serializable, Accessible, SwingConstants {
  19.    protected int tabPlacement;
  20.    protected SingleSelectionModel model;
  21.    private boolean haveRegistered;
  22.    protected ChangeListener changeListener;
  23.    Vector pages;
  24.    protected transient ChangeEvent changeEvent;
  25.    static Class class$com$sun$java$swing$event$ChangeListener;
  26.  
  27.    public JTabbedPane() {
  28.       this(1);
  29.    }
  30.  
  31.    public JTabbedPane(int tabPlacement) {
  32.       this.tabPlacement = 1;
  33.       this.changeListener = null;
  34.       this.changeEvent = null;
  35.       this.setTabPlacement(tabPlacement);
  36.       this.pages = new Vector(1);
  37.       this.setModel(new DefaultSingleSelectionModel());
  38.       this.updateUI();
  39.    }
  40.  
  41.    public Component add(Component component) {
  42.       this.addTab(component.getName(), component);
  43.       return component;
  44.    }
  45.  
  46.    public Component add(Component component, int index) {
  47.       this.insertTab(component.getName(), (Icon)null, component, (String)null, index);
  48.       return component;
  49.    }
  50.  
  51.    public void add(Component component, Object constraints) {
  52.       if (constraints instanceof String) {
  53.          this.addTab((String)constraints, component);
  54.       } else if (constraints instanceof Icon) {
  55.          this.addTab((String)null, (Icon)constraints, component);
  56.       } else {
  57.          this.add(component);
  58.       }
  59.  
  60.    }
  61.  
  62.    public void add(Component component, Object constraints, int index) {
  63.       Icon icon = constraints instanceof Icon ? (Icon)constraints : null;
  64.       String title = constraints instanceof String ? (String)constraints : null;
  65.       this.insertTab(title, icon, component, (String)null, index);
  66.    }
  67.  
  68.    public Component add(String title, Component component) {
  69.       this.addTab(title, component);
  70.       return component;
  71.    }
  72.  
  73.    public void addChangeListener(ChangeListener l) {
  74.       EventListenerList var10000 = super.listenerList;
  75.       Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  76.       if (var10001 == null) {
  77.          try {
  78.             var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  79.          } catch (ClassNotFoundException var2) {
  80.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  81.          }
  82.  
  83.          class$com$sun$java$swing$event$ChangeListener = var10001;
  84.       }
  85.  
  86.       var10000.add(var10001, l);
  87.    }
  88.  
  89.    public void addTab(String title, Icon icon, Component component) {
  90.       this.insertTab(title, icon, component, (String)null, this.pages.size());
  91.    }
  92.  
  93.    public void addTab(String title, Icon icon, Component component, String tip) {
  94.       this.insertTab(title, icon, component, tip, this.pages.size());
  95.    }
  96.  
  97.    public void addTab(String title, Component component) {
  98.       this.insertTab(title, (Icon)null, component, (String)null, this.pages.size());
  99.    }
  100.  
  101.    protected ChangeListener createChangeListener() {
  102.       return new ModelListener(this);
  103.    }
  104.  
  105.    protected void fireStateChanged() {
  106.       Object[] listeners = super.listenerList.getListenerList();
  107.  
  108.       for(int i = listeners.length - 2; i >= 0; i -= 2) {
  109.          Object var10000 = listeners[i];
  110.          Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  111.          if (var10001 == null) {
  112.             try {
  113.                var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  114.             } catch (ClassNotFoundException var3) {
  115.                throw new NoClassDefFoundError(((Throwable)var3).getMessage());
  116.             }
  117.  
  118.             class$com$sun$java$swing$event$ChangeListener = var10001;
  119.          }
  120.  
  121.          if (var10000 == var10001) {
  122.             if (this.changeEvent == null) {
  123.                this.changeEvent = new ChangeEvent(this);
  124.             }
  125.  
  126.             ((ChangeListener)listeners[i + 1]).stateChanged(this.changeEvent);
  127.          }
  128.       }
  129.  
  130.    }
  131.  
  132.    public AccessibleContext getAccessibleContext() {
  133.       if (super.accessibleContext == null) {
  134.          super.accessibleContext = new AccessibleJTabbedPane(this);
  135.       }
  136.  
  137.       return super.accessibleContext;
  138.    }
  139.  
  140.    public Color getBackgroundAt(int index) {
  141.       return ((Page)this.pages.elementAt(index)).getBackground();
  142.    }
  143.  
  144.    public Rectangle getBoundsAt(int index) {
  145.       return super.ui != null ? ((TabbedPaneUI)super.ui).getTabBounds(this, index) : null;
  146.    }
  147.  
  148.    public Component getComponentAt(int index) {
  149.       return ((Page)this.pages.elementAt(index)).component;
  150.    }
  151.  
  152.    public Icon getDisabledIconAt(int index) {
  153.       return ((Page)this.pages.elementAt(index)).disabledIcon;
  154.    }
  155.  
  156.    public Color getForegroundAt(int index) {
  157.       return ((Page)this.pages.elementAt(index)).getForeground();
  158.    }
  159.  
  160.    public Icon getIconAt(int index) {
  161.       return ((Page)this.pages.elementAt(index)).icon;
  162.    }
  163.  
  164.    public SingleSelectionModel getModel() {
  165.       return this.model;
  166.    }
  167.  
  168.    public Component getSelectedComponent() {
  169.       int index = this.getSelectedIndex();
  170.       return index == -1 ? null : this.getComponentAt(index);
  171.    }
  172.  
  173.    public int getSelectedIndex() {
  174.       return this.model.getSelectedIndex();
  175.    }
  176.  
  177.    public int getTabCount() {
  178.       return this.pages.size();
  179.    }
  180.  
  181.    public int getTabPlacement() {
  182.       return this.tabPlacement;
  183.    }
  184.  
  185.    public int getTabRunCount() {
  186.       return super.ui != null ? ((TabbedPaneUI)super.ui).getTabRunCount(this) : 0;
  187.    }
  188.  
  189.    public String getTitleAt(int index) {
  190.       return ((Page)this.pages.elementAt(index)).title;
  191.    }
  192.  
  193.    public String getToolTipText(MouseEvent event) {
  194.       if (super.ui != null) {
  195.          int index = ((TabbedPaneUI)super.ui).tabForCoordinate(this, event.getX(), event.getY());
  196.          if (index != -1) {
  197.             return ((Page)this.pages.elementAt(index)).tip;
  198.          }
  199.       }
  200.  
  201.       return super.getToolTipText(event);
  202.    }
  203.  
  204.    public TabbedPaneUI getUI() {
  205.       return (TabbedPaneUI)super.ui;
  206.    }
  207.  
  208.    public String getUIClassID() {
  209.       return "TabbedPaneUI";
  210.    }
  211.  
  212.    public int indexOfComponent(Component component) {
  213.       for(int i = 0; i < this.getTabCount(); ++i) {
  214.          if (this.getComponentAt(i).equals(component)) {
  215.             return i;
  216.          }
  217.       }
  218.  
  219.       return -1;
  220.    }
  221.  
  222.    public int indexOfTab(Icon icon) {
  223.       for(int i = 0; i < this.getTabCount(); ++i) {
  224.          if (this.getIconAt(i).equals(icon)) {
  225.             return i;
  226.          }
  227.       }
  228.  
  229.       return -1;
  230.    }
  231.  
  232.    public int indexOfTab(String title) {
  233.       for(int i = 0; i < this.getTabCount(); ++i) {
  234.          if (this.getTitleAt(i).equals(title == null ? "" : title)) {
  235.             return i;
  236.          }
  237.       }
  238.  
  239.       return -1;
  240.    }
  241.  
  242.    public void insertTab(String title, Icon icon, Component component, String tip, int index) {
  243.       Icon disabledIcon = null;
  244.       if (icon != null && icon instanceof ImageIcon) {
  245.          disabledIcon = new ImageIcon(GrayFilter.createDisabledImage(((ImageIcon)icon).getImage()));
  246.       }
  247.  
  248.       this.pages.insertElementAt(new Page(this, this, title != null ? title : "", icon, disabledIcon, component, tip), index);
  249.       if (component != null) {
  250.          component.setVisible(false);
  251.          ((Container)this).addImpl(component, (Object)null, -1);
  252.       }
  253.  
  254.       ((Container)this).invalidate();
  255.       if (this.pages.size() == 1) {
  256.          this.setSelectedIndex(0);
  257.       }
  258.  
  259.       if (!this.haveRegistered && tip != null) {
  260.          ToolTipManager.sharedInstance().registerComponent(this);
  261.          this.haveRegistered = true;
  262.       }
  263.  
  264.       if (super.accessibleContext != null) {
  265.          super.accessibleContext.firePropertyChange("AccessibleVisibleData", (Object)null, component);
  266.       }
  267.  
  268.    }
  269.  
  270.    public boolean isEnabledAt(int index) {
  271.       return ((Page)this.pages.elementAt(index)).isEnabled();
  272.    }
  273.  
  274.    public void remove(Component component) {
  275.       int index = this.indexOfComponent(component);
  276.       if (index != -1) {
  277.          this.removeTabAt(index);
  278.       }
  279.  
  280.    }
  281.  
  282.    public void removeAll() {
  283.       this.setSelectedIndex(-1);
  284.       int tabCount = this.getTabCount();
  285.  
  286.       for(int i = 0; i < tabCount; ++i) {
  287.          Component component = this.getComponentAt(i);
  288.          if (super.accessibleContext != null) {
  289.             super.accessibleContext.firePropertyChange("AccessibleVisibleData", component, (Object)null);
  290.          }
  291.       }
  292.  
  293.       super.removeAll();
  294.       this.pages.removeAllElements();
  295.    }
  296.  
  297.    public void removeChangeListener(ChangeListener l) {
  298.       EventListenerList var10000 = super.listenerList;
  299.       Class var10001 = class$com$sun$java$swing$event$ChangeListener;
  300.       if (var10001 == null) {
  301.          try {
  302.             var10001 = Class.forName("com.sun.java.swing.event.ChangeListener");
  303.          } catch (ClassNotFoundException var2) {
  304.             throw new NoClassDefFoundError(((Throwable)var2).getMessage());
  305.          }
  306.  
  307.          class$com$sun$java$swing$event$ChangeListener = var10001;
  308.       }
  309.  
  310.       var10000.remove(var10001, l);
  311.    }
  312.  
  313.    public void removeTabAt(int index) {
  314.       int tabCount = this.getTabCount();
  315.       int selected = this.getSelectedIndex();
  316.       if (selected >= tabCount - 1) {
  317.          this.setSelectedIndex(selected - 1);
  318.       }
  319.  
  320.       Component component = this.getComponentAt(index);
  321.       if (super.accessibleContext != null) {
  322.          super.accessibleContext.firePropertyChange("AccessibleVisibleData", component, (Object)null);
  323.       }
  324.  
  325.       if (component != null) {
  326.          super.remove(component);
  327.       }
  328.  
  329.       this.pages.removeElementAt(index);
  330.       ((Container)this).invalidate();
  331.    }
  332.  
  333.    public void setBackgroundAt(int index, Color background) {
  334.       ((Page)this.pages.elementAt(index)).setBackground(background);
  335.       ((Component)this).repaint();
  336.    }
  337.  
  338.    public void setComponentAt(int index, Component component) {
  339.       Page page = (Page)this.pages.elementAt(index);
  340.       if (component != page.component) {
  341.          if (page.component != null) {
  342.             synchronized(((Component)this).getTreeLock()){}
  343.  
  344.             try {
  345.                int count = ((Container)this).getComponentCount();
  346.                Component[] children = ((Container)this).getComponents();
  347.  
  348.                for(int i = 0; i < count; ++i) {
  349.                   if (children[i] == page.component) {
  350.                      ((Container)this).remove(i);
  351.                   }
  352.                }
  353.             } catch (Throwable var9) {
  354.                throw var9;
  355.             }
  356.          }
  357.  
  358.          page.component = component;
  359.          ((Container)this).addImpl(component, (Object)null, -1);
  360.       }
  361.  
  362.    }
  363.  
  364.    public void setDisabledIconAt(int index, Icon disabledIcon) {
  365.       ((Page)this.pages.elementAt(index)).disabledIcon = disabledIcon;
  366.       if (!this.isEnabledAt(index)) {
  367.          ((Container)this).invalidate();
  368.       }
  369.  
  370.    }
  371.  
  372.    public void setEnabledAt(int index, boolean enabled) {
  373.       ((Page)this.pages.elementAt(index)).setEnabled(enabled);
  374.       ((Component)this).repaint();
  375.    }
  376.  
  377.    public void setForegroundAt(int index, Color foreground) {
  378.       ((Page)this.pages.elementAt(index)).setForeground(foreground);
  379.       ((Component)this).repaint();
  380.    }
  381.  
  382.    public void setIconAt(int index, Icon icon) {
  383.       Icon oldIcon = ((Page)this.pages.elementAt(index)).icon;
  384.       ((Page)this.pages.elementAt(index)).icon = icon;
  385.       ((Container)this).invalidate();
  386.       this.getAccessibleContext();
  387.       if (oldIcon != icon && super.accessibleContext != null) {
  388.          super.accessibleContext.firePropertyChange("AccessibleVisibleData", oldIcon, icon);
  389.       }
  390.  
  391.    }
  392.  
  393.    public void setModel(SingleSelectionModel model) {
  394.       SingleSelectionModel oldModel = this.getModel();
  395.       if (oldModel != null) {
  396.          oldModel.removeChangeListener(this.changeListener);
  397.          this.changeListener = null;
  398.       }
  399.  
  400.       this.model = model;
  401.       if (model != null) {
  402.          this.changeListener = this.createChangeListener();
  403.          model.addChangeListener(this.changeListener);
  404.       }
  405.  
  406.       ((JComponent)this).firePropertyChange("model", oldModel, model);
  407.    }
  408.  
  409.    public void setSelectedComponent(Component c) {
  410.       int index = this.indexOfComponent(c);
  411.       if (index != -1) {
  412.          this.setSelectedIndex(index);
  413.       } else {
  414.          throw new IllegalArgumentException("component not found in tabbed pane");
  415.       }
  416.    }
  417.  
  418.    public void setSelectedIndex(int index) {
  419.       int oldIndex = this.model.getSelectedIndex();
  420.       this.model.setSelectedIndex(index);
  421.       if (oldIndex >= 0 && oldIndex != index) {
  422.          Page var10000 = (Page)this.pages.elementAt(oldIndex);
  423.          if (super.accessibleContext != null) {
  424.             super.accessibleContext.firePropertyChange("AccessibleState", AccessibleState.SELECTED, (Object)null);
  425.          }
  426.       }
  427.  
  428.       if (index >= 0 && oldIndex != index) {
  429.          Page var3 = (Page)this.pages.elementAt(index);
  430.          if (super.accessibleContext != null) {
  431.             super.accessibleContext.firePropertyChange("AccessibleState", (Object)null, AccessibleState.SELECTED);
  432.          }
  433.       }
  434.  
  435.    }
  436.  
  437.    public void setTabPlacement(int tabPlacement) {
  438.       if (tabPlacement != 1 && tabPlacement != 2 && tabPlacement != 3 && tabPlacement != 4) {
  439.          throw new IllegalArgumentException("illegal tab placement: must be TOP, BOTTOM, LEFT, or RIGHT");
  440.       } else {
  441.          if (this.tabPlacement != tabPlacement) {
  442.             int old = this.tabPlacement;
  443.             this.tabPlacement = tabPlacement;
  444.             ((JComponent)this).firePropertyChange("tabPlacement", old, tabPlacement);
  445.             ((Container)this).invalidate();
  446.          }
  447.  
  448.       }
  449.    }
  450.  
  451.    public void setTitleAt(int index, String title) {
  452.       String oldTitle = ((Page)this.pages.elementAt(index)).title;
  453.       ((Page)this.pages.elementAt(index)).title = title;
  454.       ((Container)this).invalidate();
  455.       if (oldTitle != title && super.accessibleContext != null) {
  456.          super.accessibleContext.firePropertyChange("AccessibleVisibleData", oldTitle, title);
  457.       }
  458.  
  459.    }
  460.  
  461.    public void setUI(TabbedPaneUI ui) {
  462.       super.setUI(ui);
  463.    }
  464.  
  465.    public void updateUI() {
  466.       int count = this.getTabCount();
  467.  
  468.       while(count-- > 0) {
  469.          Page page = (Page)this.pages.elementAt(count);
  470.          Component component = page.component;
  471.          if (component.getParent() != this && component instanceof Container) {
  472.             page.needsUIUpdate = true;
  473.          } else {
  474.             page.needsUIUpdate = false;
  475.          }
  476.       }
  477.  
  478.       this.setUI((TabbedPaneUI)UIManager.getUI(this));
  479.    }
  480. }
  481.